//
//  ZXLSecurityClient.m
//  SignSecurityDemo
//
//  Created by zxl02 on 2020/8/14.
//  Copyright © 2020 CordovaDemo. All rights reserved.
//

#import "ZXLSecurityClient.h"
@interface ZXLSecurityClient ()
@property(nonatomic, strong, readonly) ZXLSignTools *signTools;
@end
@implementation ZXLSecurityClient
#pragma mark - public

static ZXLSecurityClient *client = nil;

+ (instancetype)sharedClient {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        client = [[self alloc] init];
    });
    return client;
}

- (instancetype)init {
    if (self = [super init]) {
        _apiManager= [[ZXLAPISecurityManager alloc] init];
        _fileManager = [[ZXLFileSecurityManager alloc] init];
        _securityConfig=[[ZXLConfig alloc]init];
    }
    return self;
}
-(SignResult*)getsignResultStrWithScene:(SecuritySceneType)sceneType  signEntity:(ZXLSignData*)signEntity{
    SignResult * result=[SignResult new];
         if (!sceneType) {
             result.msg=@"请设置SecuritySceneType";   return result;
         }
         switch (sceneType) {
                 case SecurityTypeAPI:
                 result = [self.apiManager getAPIServiceResultWith:signEntity config:self.securityConfig];
                 break;
                 case SecurityTypeFile:
                 result = [self.fileManager getFile:signEntity FileSignType:self.securityConfig.fileType zxlFileConfig:self.securityConfig];
                 break;
                 case SecurityTypeShare:
                 break;
                 case SecurityTypeCustom:
                 break;
             default:
                 break;
         }
         return result;
}




//获取当前时间戳
- (NSString *)currentTimeStr {
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time=[date timeIntervalSince1970]*1000;// *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
    return timeString;
}
//获取随机数
- (NSString *)getArc4random {
    return [NSString stringWithFormat:@"%d",(arc4random() % 1000001) + 1000000];
}



-(NSString*)getSignatureWithSignURL:(NSString*)signURL signBody:(NSDictionary*)signBody signType:(NSInteger)signType{
    NSString * strToSign;
    if (signType==1) {
        //1. application/x-www-form-urlencoded 格式
        strToSign=  [NSString stringWithFormat:@"%@%@%@",signURL,@"\n",[ZXLSignTools getStringWithDic:(NSMutableDictionary*)signBody]];
    }else{
        NSString* md5body=   [ZXLSignTools getMd5_32Bit_String:[ZXLSignTools JSONString:signBody] isUppercase:NO];
        strToSign= [NSString stringWithFormat:@"%@%@%@%@%@",signURL,@"\n",[ZXLSignTools  getStringWithDic:(NSMutableDictionary*)signBody],@"\n",md5body];
    }
    NSString *timestamp = [self currentTimeStr];
    NSString *random100 = [self getArc4random];
    strToSign = [NSString stringWithFormat:@"%@+%@+%@+%@+%@+%@+%@",strToSign,@"\n",timestamp,@"\n",random100,@"\n",strToSign];
    return  strToSign =[ZXLSignTools getMd5_32Bit_String:strToSign isUppercase:NO];
}


/*
- (void)RSASecuritySign:(SecuritySignMethod)method text:(NSString*)text  Key:(NSString*)key keyType:(BOOL)keyType completion:(SignResultBlock)completion{
    SignResult * model =[SignResult new];
    if (!key) {
        [self NUllKey:completion];
        return;
    }
    NSString *signStr;
    NSInteger  code;
    if(method==SecuritySignEncrypt) {
        signStr =  keyType?[ZXLSignToolRSA encryptString:text publicKey:key]:[ZXLSignToolRSA encryptString:text privateKey:key];
        if (signStr) {
        }else{
            signStr=@"加密错误";
        }
    }else{
        signStr = keyType?[ZXLSignToolRSA decryptString:text publicKey:key]:[ZXLSignToolRSA decryptString:text privateKey:key];
        if (signStr) {
        }else{
            signStr=@"解密错误";
        }
    }
    model.successCode=code;
    model.signResultStr=model.msg=signStr;
    completion(model);
}
- (void)DESSecuritySign:(SecuritySignMethod)method is3DES:(BOOL)is3DES text:(NSString*)text  Key:(NSString*)key completion:(SignResultBlock)completion{
    SignResult * model =[SignResult new];
      if (!key) {
          [self NUllKey:completion];
          return;
      }
    NSString *signStr;
    if (is3DES) {
        if(method==SecuritySignEncrypt){
          signStr=  [self.signToolDES encryptUse3DES:text key:key];
        }else{
           signStr=  [self.signToolDES decryptUse3DES:text key:key];
        }
    }else{
        if(method==SecuritySignEncrypt){
            signStr=  [self.signToolDES encryptUseDES:text key:key];
        }else{
            signStr=  [self.signToolDES decryptUseDES:text key:key];
        }
    }
    model.signResultStr=model.msg=signStr;
    completion(model);
}
-(void)AESSecuritySign:(SecuritySignMethod)method text:(NSString*)text  Key:(NSString*)key keySize:(SecuritySignLengthAES)keySize completion:(SignResultBlock)completion{
    SignResult * model =[SignResult new];
    if (!key) {
        [self NUllKey:completion];
        return;
    }
    NSString *signStr;
    if(method==SecuritySignEncrypt) {
        signStr= [self.signToolAES AESEncrypt:text key:key keySize:keySize];
    }else{
        signStr= [self.signToolAES AESDecrypt:text key:key keySize:keySize];
    }
    model.signResultStr=model.msg=signStr;
    completion(model);
}

-(void)NUllKey:(SignResultBlock)completion{
    SignResult * model =[SignResult new];
    model.successCode=999;
    model.msg=@"key不存在";
    completion(model);
}



-(NSString*)getSignatureWithSignURL:(NSString*)signURL signBody:(NSDictionary*)signBody signType:(NSInteger)signType{
    NSString * strToSign;
    if (signType==1) {
        //1. application/x-www-form-urlencoded 格式
        strToSign=  [NSString stringWithFormat:@"%@+%@+%@",signURL,@"\n",[self.signTools getStringWithDic:(NSMutableDictionary*)signBody]];
    }else{
        NSString* md5body= [self.signTools getMd5_32Bit_String:[_signTools JSONString:signBody] isUppercase:NO];
        strToSign= [NSString stringWithFormat:@"%@+%@+%@+%@+%@",signURL,@"\n",[self.signTools  getStringWithDic:(NSMutableDictionary*)signBody],@"\n",md5body];
    }
    NSString *timestamp = [self currentTimeStr];
    NSString *random100 = [self getArc4random];
    strToSign = [NSString stringWithFormat:@"%@+%@+%@+%@+%@+%@+%@",strToSign,@"\n",timestamp,@"\n",random100,@"\n",strToSign];
    return  strToSign =[self.signTools getMd5_32Bit_String:strToSign isUppercase:NO];
}*/
@end
